import { createRouter,createWebHashHistory } from "vue-router";
import { ElMessage } from 'element-plus';
const routes = [
  {
    path: "/",
    redirect: "/login",
    meta: {
      title: "万事皆可期待"
    },
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "万事皆可期待"
    },
    component: () => import("../views/login/login.vue")
  },
  {
    path: "/home",
    name: "Home",
    component: () => import("../views/home/home.vue"),
    redirect: "/overview/summary",
    children: [
      {
        path: "/overview/summary",
        meta: {
          title: "我想你了"
        },
        component: () => import("../components/overview/overview.vue")
      },
      {
        path: "/user/list",
        meta: {
          title: "要一直在一起"
        },
        component: () => import("../components/user/userList/userlist.vue")
      },
      {
        path: "/user/add",
        meta: {
          title: "爱你呦"
        },
        component: () => import("../components/user/userAdd/userAdd.vue")
      },
      {
        path: "/data/add",
        meta: {
          title: "记录我们的点点滴滴"
        },
        component: () => import("../components/data/dataAdd/dataAdd.vue")
      },
      {
        path: "/data/list",
        meta: {
          title: "记录我们的点点滴滴"
        },
        component: () => import("../components/data/dataList/dataList.vue")
      },
      {
        path: "/control/user",
        meta: {
          title: "记录我们的点点滴滴"
        },
        component: () => import("../components/control/control.vue")
      },
      {
        path: "/expense/expenseAdd",
        meta: {
          title: "看看我们能花多少钱吧"
        },
        component: () => import("../components/expense/expenseAdd/expenseAdd.vue")
      },
      {
        path: "/expense/expenseList",
        meta: {
          title: "嘿嘿、有没有意外"
        },
        component: () => import("../components/expense/expenseList/expenseList.vue")
      },
      {
        path: "/expense/details",
        meta: {
          title: "嘿嘿、有没有意外"
        },
        component: () => import("../components/expense/expenseData/expenseData.vue")
      },
      {
        path: "/chat/family",
        meta: {
          title: "有你才有家"
        },
        component: () => import("../components/chatroom/chatroom.vue")
      },
      {
        path: "/email/send",
        meta: {
          title: "发送系统通知"
        },
        component: () => import("../components/system_email/sendEmail/sendEmail.vue")
      },
      {
        path: "/system/message",
        meta: {
          title: "系统通知"
        },
        component: () => import("../components/system_email/emailList/emailList.vue")
      },
      {
        path: "/updateimg/add",
        meta: {
          title: "你是最美的"
        },
        component: () => import("../components/image/upload/upload.vue")
      },
      {
        path: "/updateimg/find",
        meta: {
          title: "你看、漂亮吧"
        },
        component: () => import("../components/image/imageList/imageList.vue")
      },
      {
        path: "/luck/luckMeet",
        meta: {
          title: "结缘"
        },
        component: () => import("../components/luckMeet/luckMeet.vue")
      },
    ]
  },
  {
    path: "/:id",
    meta: {
      title: "emmm....."
    },
    component: () => import("@/components/error/error.vue")
  }
];

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes,
});

router.beforeEach((to, from, next) => {
  let userToken = JSON.parse(sessionStorage.getItem("userinfo"));
  console.log(to.path, userToken);
  if (to.path == "/login") {
    next();
  } else {
    if (userToken) {
      if (to.meta.title) {
        document.title = to.meta.title;
      }
      next();
    } else {
      ElMessage.warning("你还没有登录哦、请先登录把");
      router.push("/login");
    }
  }

  // if (to.meta.title) {
  //   document.title = to.meta.title;
  // }
  // next();
})

export default router;
